import axios from "axios"

export const mobileValidator = (r, v, c) => {
    if (!v || v === ''){
        c(new Error('请输入手机号码'));
        return;
    }else if (!/^1\d{10}$/.test(v)) {
        c(new Error('手机号码不合法.'));
        return;
    }
    c()
}

export const phoneValidator = (r, v, c) => {
    if (v && v !== '') {
        if (!/^(\d{3,4}\-\d{7,8})$/.test(v)) {
            c(new Error('固话格式不正确.'))
            return;
        }
    }
    c()
}

export const isEan13Code = (v)=>{
    var sum, sum1 = 0, sum2 = 0;
    if(!/^\d{13}$/.test(v)) return false;
    for(var i=0;i<12;i+=2)
        sum1 += parseInt(v[i]);
    for(var i=1;i < 13;i+=2)
        sum2 += parseInt(v[i]);
    sum = 10 - ((sum1 + sum2 * 3) % 10);
    return sum == parseInt(v[12]);
}

export const checkEan13 = (r,v,c)=>{
    if(!v || v==''){return c();}
    if(isEan13Code(v))
        return c();
    return c(new Error(r.message ??'商品条码校验不通过.'));
}
export const checkInside = (r,v,c)=>{
    if(!v || v==''){return c();}
    if(!/^\d{10}$/.test(v)) return false;
    return c(new Error(r.message ??'商品条码校验不通过.'));
}

export const cafeIDValidator = (r, v, c) => {
    if (!v || v == '')
        c(new Error('场所代码必填.'));
    else if (!/^\d{10}$/.test(v))
        c('场所代码不合法.');
    else
        c()
}

export const idCardValidator = (r, v, c) => {
    if (!/^\d{17}(\d|x)$/i.test(v)){
        c(new Error('身份证号码格式不正确.'));
        return;
    }

    //生日校验
    var birthday = v.substr(6, 4) + '/' + Number(v.substr(10, 2)) + '/' + Number(v.substr(12, 2));
    var d = new Date(birthday);
    var newBirthday = d.getFullYear() + '/' + Number(d.getMonth() + 1) + '/' + Number(d.getDate());
    var currentTime = new Date().getTime();

    if (d.getTime() >= currentTime || birthday !== newBirthday){
        c(new Error('身份证出生年月不正确.'));
        return;
    }

    //奇偶校验
    var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    var sum = 0, i, residue;
    for (i = 0; i < 17; i++) {
        sum += v.substr(i, 1) * arrInt[i];
    }
    residue = arrCh[sum % 11];
    if (residue !== v.substr(17, 1)){
        c(new Error('身份证校验不通过.'));
        return;
    }
    c()
}
export const repasswordValidator = (r, v, c) => {
    let v1 = v || '', v2 = r.compareTo || '';
    //console.log(v1 ,':', v2)
    if (v1 !== v2){
        c(new Error('两次输入的密码不一致.'))
        return;
    }
    c()
}


export const validateHotelName = (rule, value, callback) => {
    if (value == '') {
        callback(new Error('请输入酒店名称'))
        return;
    } else {
        let reg = /^[\u4e00-\u9fa5A-Za-z]+$/
        if (!reg.test(value)) {
            callback(new Error('酒店名称只能包含中文和英文字母'))
            return;
        }
    }
    callback();
}

export const unique = (rule, value, cb)=>{
    let opt = {type:rule.type, value:value};

    if(rule.id){
        cb();
    }else{
        if(['number', 'string'].indexOf(typeof rule['id']) != -1){
            opt['id'] = rule.id;
        }
        axios.post(rule.url ?? '/netbar/unique', opt).then(res=>{
            if(200 !== res.status)
                cb(new Error('服务暂时不可用'));
            else{
                if(rule.cb){
                    cb(rule.cb(res.data))
                }else{
                    if(false == res.data.success)
                        cb(new Error(res.data.message));
                    else
                        cb();
                }
            }
        });
    }
}


export const range = (rule, value, cb)=>{
    if(''===value || undefined === value || null === value){
        cb();
        return
    }
    if((value < rule.min || value > rule.max)){
        let msg =  `超出允许范围:${rule.min},${rule.max}`;
        cb(new Error(msg));
        return;
    }
    cb();
}
export const checkLarger = (rule, value, c)=>{
    let v = parseFloat(value);
    if(isNaN(value)) return true;
    if(v < rule.min){
        c(new Error(`必须大于 ${rule.min}`))
        return;
    }
    c()
}
export const checkEGT = (r,v,c)=>{
    if(v < r.val){ c(r.message)}
    c()
}


export const checkRmb = (rule, value, c)=>{
    if(!value) return true;
    if(value !== undefined && !/^\d+(\.\d{1,2}){0,1}$/.test(value))
        return c(new Error(rule.message || '￥格式不合法.'));
    c();
}

export const checkTimeLength = (rule, value, c)=>{
    if(!value) return true;
    let v = [1440, 60, 1][rule.unit()] * value;
    if(v < rule.min) return c(new Error(rule.message || '时长太短了'));
    c();
}

export const checkWanjia = (rule, value, c)=>{
    if(!value) return true;
    if(!/^\d+(\.\d{1}){0,1}$/.test(value))
        c(new Error(rule.message || '格式不合法.'));
    if(value % 0.5 !== 0){
        c(new Error(rule.message || '必须是 0.5元的倍数.'));
    }
    return true;
}

export const checkInt = (rule, value, c)=>{
    if(!/^\d+$/.test(value))
        c(new Error(rule.message || '必须是整数.'))
    return true;
}

export const checkNumber = (rule, value, c)=>{
    if(!/^(\d*)(\.\d+)*$/.test(value))
        c(new Error(rule.message || '必须是整数.'))
    return true;
}

export const iptFloat = (e)=>{
    console.log(e);
    return '';
}

export const callbackValidator  = (r, v, c)=>{
    if(false == r.cb(r, v, c)){
        c(new Error(r.message ?? '验证不通过.'))
        return;
    }
    c()
}

export const checkSelNum = (rule, value, cb)=>{
    if(value instanceof Array && value.length >= rule.min){
        cb();
    }
    cb(new Error(rule.message || '至少选择'+ rule.min+'项.'))
}